package com.zb.controller;

import com.zb.entity.TbOrderItemModel;
import com.zb.service.CartService;
import com.zb.util.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @date:2024/11/20 15:49 星期三
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加到购物车
     * @param id 商品id（skuId）
     * @param num 商品数量
     * @return
     */
    @GetMapping("/add")
    public Boolean addCart(@RequestParam("id")String id,@RequestParam("num")Integer num){
        //从token获取用户名
        String username = TokenDecode.getUserInfo().get("username");
        return cartService.addItem(id, num,username);
    }

    @GetMapping("/list")
    public List<TbOrderItemModel> list(){
        String username = TokenDecode.getUserInfo().get("username");
        return cartService.list(username);
    }



    @GetMapping("/select")
    private List<TbOrderItemModel> del(@RequestParam("skuIds")String [] skuIds){
        String username = TokenDecode.getUserInfo().get("username");
        return cartService.select(username,skuIds);
    }

    @GetMapping("/remove")
    public Boolean remove(@RequestParam("skuId") String skuId) {
        String username = TokenDecode.getUserInfo().get("username");
        return cartService.remove(username, skuId);
    }


}
